package com.ibeeking.gmc.global.g.rest.config.prop;


import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;

import java.io.Serializable;

/**
 * @Author ibeeking
 * Data : 17:28
 */
@Data
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties extends JedisPoolConfig implements Serializable {
    private static final long serialVersionUID = -5924815351660074401L;

    @Value("${spring.redis.host:127.0.0.1}")
    public String host;

    @Value("${spring.redis.port:3306}")
    public Integer port;

    @Value("${spring.redis.password:123456}")
    public String password;

    @Value("${spring.redis.database:0}")
    private int database;

    private int connectionTimeout = Protocol.DEFAULT_TIMEOUT;

    private int timeout = Protocol.DEFAULT_TIMEOUT;

    private String clientName;

    private boolean ssl;

    private SSLSocketFactory sslSocketFactory;

    private SSLParameters sslParameters;

    private HostnameVerifier hostnameVerifier;

    private Jedis shardedJedisPool(int dbIndex) {
        this.database = dbIndex;
        JedisPool jedisPool = new JedisPool(this, host, port, timeout,password, database);
        return jedisPool.getResource();
    }
    public JedisPool getJedisPool() {
        JedisPool jedisPool = new JedisPool(this, host, port, timeout,password, database);
        return jedisPool;
    }

    public String get(String key, int dbIndex) {
        Jedis jedis = shardedJedisPool(dbIndex);
        Object obj = jedis.get(key);
        if(jedis != null)
            jedis.close();
        return obj.toString();
    }

}